import React, { useState,useEffect,useRef, forwardRef, useImperativeHandle } from 'react';
import GenericTable from '../components/tables/GenericTable';
import { Tag,Switch,message } from 'antd';
import axios from 'axios';
import '../css/lzk.css';
import dayjs from 'dayjs';
axios.defaults.baseURL = 'http://localhost:3000/'
// 定义表格列配置



// 处理邀请操作的函数
const handleInvite = (record) => {
    console.log(`Inviting ${record.name}`);
};
const handleEdit=async()=>{
    console.log('编辑')
}
// 处理删除操作的函数
const handleDelete = async (id, onReload) => {
    try {
        await axios.get('/kai/delslide', { params: { _id: id } });
        message.error('删除成功');
        if (onReload) {
            onReload(); // 调用父组件传递的函数，触发重新渲染
        }
    } catch (error) {
        message.error('删除失败');
    }
};

const DataProvider = (props) => {
    const columns = [
    {
        align:'center',
        title: '序号',
        dataIndex: 'index',
        key: '_id',
        render: (_, record, index) => index + 1
    },
    {
        align:'center',
        title: 'Banner图',
        key: '_id',
        render: (_, record) => (
            <img src={record.img} style={{width:'3rem',height:'60px',marginTop:'0.1rem',borderRadius:'0.3rem'}} alt="" />
        )
    },
    {
        width:'6.4rem',
        align:'center',
        title: '标题',
        key: '_id',
        render: (_, record) => (
              <span>{record.title}</span>
        )
    },
    {
        align:'center',
        title: '最后更新人',
        key: '_id',
        render: (_, record) => (
              <span>{record.name}</span>
        )
    },
   
    {
        align:'center',
        title: '最后更新时间',
        key: '_id',
        render: (_, record)=>(
            dayjs(record.start_time).format('YYYY-MM-DD HH:mm:ss')
        )
    },
    {
        align:'center',
        title: '状态',
        key: '_id',
        dataIndex: 'status',
        render:(_, record)=>(
            <Switch checkedChildren="启用" unCheckedChildren="禁用" checked={record.status} />
        )
    },
    {
        align:'center',
        title: '操作',
        render: (_, record) => (
            <span>
                <a onClick={() => handleEdit(record._id)} style={{ marginLeft: 16 }}>
                    编辑
                </a>
                <a onClick={() => handleDelete(record._id,props.onReload)} style={{ marginLeft: 16,color:'red' }}>
                    删除
                </a>
            </span>
        )
    },

];
    return (
        <GenericTable
            columns={columns}
            dataSource={props.slideshowList}
            onInvite={handleInvite}
            onDelete={handleDelete}
        />
    );
};

export default DataProvider;